/**
 * Project:   warnme-server
 * File:      ProfileRestWS.java
 * License: 
 *            This file is licensed under GNU General Public License version 3
 *            http://www.gnu.org/licenses/gpl-3.0.txt
 *
 * Copyright: Bartosz Cichecki [ cichecki.bartosz@gmail.com ]
 * Date:      19-03-2014
 */

package dtu.ds.warnme.ws.rest.json.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import dtu.ds.warnme.exceptions.impl.ServiceException;
import dtu.ds.warnme.services.UsersService;
import dtu.ds.warnme.utils.SecurityUtils;
import dtu.ds.warnme.ws.rest.json.AbstractRestWS;
import dtu.ds.warnme.ws.rest.json.gson.GsonHolder;
import dtu.ds.warnme.ws.rest.json.utils.RestUtils;

/**
 * @author Bartosz Cichecki
 */
@Controller
@RequestMapping(value = "/profiles")
public class ProfileRestWS extends AbstractRestWS {

	@Autowired
	private UsersService usersService;

	@PreAuthorize("hasRole('USER')")
	@RequestMapping(value = "/my", method = RequestMethod.GET, produces = RestUtils.CONTENT_APPLICATION_JSON_UTF8)
	public @ResponseBody
	String getProfile(HttpServletRequest request, HttpServletResponse response) throws ServiceException {
		String json = GsonHolder.getGson(GsonHolder.RESTRICTED).toJson(SecurityUtils.getCurrentUser());
		RestUtils.decorateResponseHeaderWithMD5(response, json);
		return json;
	}

}
